gh-90092: Support multiple terminals in the curses module#151748
Open
serhiy-storchaka wants to merge 8 commits into
Open
gh-90092: Support multiple terminals in the curses module#151748serhiy-storchaka wants to merge 8 commits into
serhiy-storchaka wants to merge 8 commits into
Conversation
Add the X/Open Curses SCREEN API for driving more than one terminal: newterm() and set_term(), plus the ncurses extension new_prescr(). A new screen object wraps the C SCREEN. It exposes the terminal's standard window as screen.stdscr. Each window keeps a reference to its screen (like a subwindow does to its parent window), so the screen is deleted automatically once it and all of its windows are unreferenced. The ncurses use_screen()/use_window() locking helpers are exposed as the screen.use() and window.use() methods. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Documentation build overview
4 files changed± library/curses.html± library/tkinter.html± whatsnew/3.16.html± whatsnew/changelog.html |
| that can be used to call functions that affect global state | ||
| before :func:`initscr` or :func:`newterm` is called. | ||
|
|
||
| Availability: if the underlying curses library provides ``new_prescr()``. |
Member
There was a problem hiding this comment.
Should we not be using the directive?
Member
Author
There was a problem hiding this comment.
No, it only supports limited set of OS names.
Member
Author
|
Doc failures require merging #151643 first. |
25df3f0 to
3be2f8f
Compare
ScreenTests drives curses over pseudo-terminals whose master ends are never read. On macOS (unlike Linux) the tcdrain() that curses performs inside endwin(), and even a plain write(), blocks once the unread output fills the pty buffer, so the test hung until the timeout. Drain the masters synchronously before endwin(), leaving room for its output. Also release the GIL around the endwin() call, so that it no longer blocks other threads while it talks to the terminal. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
3be2f8f to
d27487e
Compare
The synchronous drain before endwin() is not enough on macOS: endwin()'s own output can again exceed the pty buffer, so its tcdrain() blocks even after the buffer was emptied. Drain each master continuously from a background thread instead; the endwin() GIL release lets that thread run while endwin() blocks. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
On macOS, closing the pty slave while the master is still open blocks until the slave's pending output drains. Closing the master first stops the reader thread and leaves nothing for the slave close to wait for. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
On macOS, closing either end of a pty while a thread is blocked in read() on the master hangs. Drain with a poll() loop that a stop Event can interrupt, and stop and join the reader before closing the descriptors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Each test's newterm() screen forms a window<->screen reference cycle, reclaimed only by the cyclic GC. Collect it while the screen is still current, so the windows' delwin() succeeds; collected later, on a non-current screen, it fails (an unraisable error that trips --fail-env-changed on macOS). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
A screen and its standard window reference each other, so a screen is only reclaimed by the cyclic garbage collector. screen.close() detaches the standard window -- clearing the cycle and the window so its delwin() is skipped (delscreen() frees it instead) -- letting the screen be released by reference counting. Afterwards screen.stdscr is None and the old window raises curses.error. Use it in the tests instead of an explicit gc.collect(). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Unlike the sibling panel tests, test_userptr_segfault never removed its panel, so the window it wrapped was reachable only through a panel<->userptr __del__ cycle and was collected during a much later test, where delwin() failed on macOS (--fail-env-changed). Give it the addCleanup(self._delete_panels, ...) the other panel tests use; the per-test screen.close() in setUp then keeps the teardown free of lingering cycles. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add the X/Open Curses SCREEN API for driving more than one terminal:
curses.newterm()andcurses.set_term(), plus the ncurses extensioncurses.new_prescr().A new
screenobject wraps the CSCREENand exposes the terminal's standard window asscreen.stdscr. Each window keeps a reference to its screen (like a subwindow does to its parent window), so a screen is freed automatically once it and all of its windows are unreferenced.delscreen()is deferred to the screen object's deallocation rather than itstp_clear, so it runs only after the screen's windows — and any panels built on them — are gone.The ncurses
use_screen()/use_window()locking helpers are exposed as thescreen.use()andwindow.use()methods, forwarding*argsand**kwargsto the callable.ScreenTestsnow run in-process over pseudo-terminals instead of subprocesses, andTestCursesdrives anewterm()screen, so the new API is exercised without needing a second real terminal.🤖 Generated with Claude Code